package com.yy.controller.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.HttpStatus;
import com.yy.common.core.model.DishVM;
import com.yy.common.core.model.RestaurantVM;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.utils.PageHelper;
import com.yy.pojo.Dish;
import com.yy.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dishes-user")
public class UserDishesController {

    @Autowired
    private DishService dishService;

    @GetMapping("/query")
    public TableDataInfo queryAllDishes(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                        Dish dish) {
        IPage<Dish> page = dishService.getDishesList(pageNum, pageSize, dish);
        Page<DishVM> dishVMPage = PageHelper.copyMap(page, d -> {
            DishVM dishVM = new DishVM();
            BeanUtil.copyProperties(d,dishVM);
            return dishVM;
        });
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(dishVMPage.getRecords());
        rspData.setTotal(dishVMPage.getTotal());
        return rspData;
    }

    @GetMapping("/{id}")
    public AjaxResult selectAllDishes(@PathVariable("id") Long id) {
        Dish dish = dishService.getById(id);
        if (dish != null) {
            DishVM dishVM = new DishVM();
            BeanUtil.copyProperties(dish, dishVM);
            return AjaxResult.success(dishVM);
        } else {
            return AjaxResult.error();
        }
    }
}
